package com.ruyuan.seckilling.render.async;

import com.ruyuan.seckilling.common.ChannelKey;
import com.ruyuan.starter.async.AsyncContext;
import com.ruyuan.starter.async.BossEventBus;
import com.ruyuan.starter.async.event.BaseEvent;
import com.ruyuan.starter.async.listener.EventListener;
import com.ruyuan.starter.async.thread.ExecutorService;
import org.springframework.beans.factory.annotation.Autowired;

import static com.ruyuan.seckilling.render.async.PageRenderEventHolder.EVENT_06;

/**
 * @author zhonghuashishan
 */
public abstract class BaseRenderPageListener<E extends BaseEvent> implements EventListener<BaseEvent> {

    @Autowired
    protected BossEventBus bossEventBus;

    @Autowired
    protected ExecutorService executorService;

    @Override
    public void onEvent(BaseEvent event, AsyncContext eventContext) {
        PageRenderContext pageRenderContext = (PageRenderContext) eventContext;
        // 不正常的情况
        if (pageRenderContext.isShouldSkip()) {
            // 直接跳到第6步去保存流水
            bossEventBus.publish(ChannelKey.CHANNEL_06_SAVE_PAGE_LOG_MESSAGE, EVENT_06, pageRenderContext);
            return;
        }
        doThisStep(((E) event), pageRenderContext);
    }

    protected abstract void doThisStep(E event, PageRenderContext context);
}